import QtQuick 2.15

CountingForm {
    id: counting
    state: "pause"

    property bool flag: true

    function pause(){
        counting.flag = true
        var remaining = timer.m_remainingTime()
        timer.stop()
        timer.m_setInterval(remaining)
    }

    function resume(){
        timer.start()
    }

    timer.onTimeout: {
        var remaningSecond = parseInt(applicationFlow.settedHour) * 3600 + parseInt(applicationFlow.settedMinute) * 60 + parseInt(applicationFlow.settedSecond) - 1
        var hour = Math.floor(remaningSecond / 3600)
        var minute = Math.floor((remaningSecond % 3600) / 60)
        var second = (remaningSecond % 3600) % 60
        labelRemainingTime.text = (hour.toString().length < 2 ? "0" + hour : hour ) + ":" + (minute.toString().length < 2 ? "0" + minute : minute ) + ":" +
                (second.toString().length < 2 ? "0" + second : second )

        applicationFlow.settedHour = hour.toString()
        applicationFlow.settedMinute = minute.toString()
        applicationFlow.settedSecond = second.toString()

        if(remaningSecond == 0) {
            timer.stop()
            numberAnimation.stop()
            applicationFlow.countingStop()
            applicationFlow.state = "Setting"
            applicationFlow.dialog.open()
        }

        if(counting.flag === true) {
            timer.stop()
            timer.m_setInterval(1000);
            timer.start()
            counting.flag = false
        }

    }

    roundButtonStop.onReleased: {
        timer.stop()
        numberAnimation.stop()
        applicationFlow.countingStop()
        applicationFlow.state = "Setting"
    }

    roundButtonPauseContinue.onReleased: {
        if(state === "pause") {
            counting.pause()
            numberAnimation.pause()
            state = "continue"
        } else {
            counting.resume()
            numberAnimation.resume()
            state = "pause"
        }
    }

    states: [
        State {
            name: "pause"
            PropertyChanges {
                target: roundButtonPauseContinue
                text: "Pause"
                font.pixelSize: 50
            }
        },
        State {
            name: "continue"
            PropertyChanges {
                target: roundButtonPauseContinue
                text: "Continue"
                font.pixelSize: 40
            }
        }

    ]

    Component.onCompleted: {
        var remaningSecond = parseInt(applicationFlow.settedHour) * 3600 + parseInt(applicationFlow.settedMinute) * 60 + parseInt(applicationFlow.settedSecond)
        var hour = Math.floor(remaningSecond / 3600)
        var minute = Math.floor((remaningSecond % 3600) / 60)
        var second = (remaningSecond % 3600) % 60

        labelRemainingTime.text = (hour.toString().length < 2 ? "0" + hour : hour ) + ":" + (minute.toString().length < 2 ? "0" + minute : minute ) + ":" +
                (second.toString().length < 2 ? "0" + second : second )
        labelTotalTime.text = "共%1时%2分%3秒".arg(parseInt(applicationFlow.settedHour)).arg(parseInt(applicationFlow.settedMinute)).arg(parseInt(applicationFlow.settedSecond))

        dial.from = 0
        dial.to = remaningSecond

        numberAnimation.from = remaningSecond
        numberAnimation.to = 0
        numberAnimation.duration = remaningSecond*1000

        timer.start()
        numberAnimation.start()
    }
}
